﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Globalization
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.Activities.Rules
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities.Rules.Design
    Friend MustInherit Class BasicBrowserDialog
        Inherits Form
        ' Methods
        Protected Sub New(ByVal activity As Activity, ByVal name As String)
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            Me.activity = activity
            Me.InitializeComponent
            Me.descriptionLabel.Text = Me.DescriptionText
            Me.Text = Me.TitleText
            Me.previewLabel.Text = Me.PreviewLabelText
            Me.newRuleToolStripButton.Enabled = True
            Me.name = name
            Me.serviceProvider = activity.Site
            Dim service As IUIService = DirectCast(activity.Site.GetService(GetType(IUIService)), IUIService)
            If (Not service Is Nothing) Then
                Me.Font = DirectCast(service.Styles.Item("DialogFont"), Font)
            End If
            AddHandler MyBase.HelpRequested, New HelpEventHandler(AddressOf Me.OnHelpRequested)
            AddHandler MyBase.HelpButtonClicked, New CancelEventHandler(AddressOf Me.OnHelpClicked)
            Me.rulesListView.Select
        End Sub

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing AndAlso (Not Me.components Is Nothing)) Then
                Me.components.Dispose
            End If
            MyBase.Dispose(disposing)
        End Sub

        Protected MustOverride Function GetObjectName(ByVal ruleObject As Object) As String

        Private Sub InitializeComponent()
            Me.components = New Container
            Dim manager As New ComponentResourceManager(GetType(BasicBrowserDialog))
            Me.cancelButton = New Button
            Me.okButton = New Button
            Me.rulesListView = New ListView
            Me.nameColumnHeader = New ColumnHeader
            Me.validColumnHeader = New ColumnHeader
            Me.rulesPanel = New Panel
            Me.rulesToolStrip = New ToolStrip
            Me.imageList = New ImageList(Me.components)
            Me.newRuleToolStripButton = New ToolStripButton
            Me.editToolStripButton = New ToolStripButton
            Me.renameToolStripButton = New ToolStripButton
            Me.toolStripSeparator1 = New ToolStripSeparator
            Me.deleteToolStripButton = New ToolStripButton
            Me.preiviewPanel = New Panel
            Me.previewRichEditBoxPanel = New Panel
            Me.previewRichTextBox = New TextBox
            Me.previewLabel = New Label
            Me.descriptionLabel = New Label
            Me.headerPictureBox = New PictureBox
            Me.okCancelTableLayoutPanel = New TableLayoutPanel
            Me.rulesPanel.SuspendLayout
            Me.rulesToolStrip.SuspendLayout
            Me.preiviewPanel.SuspendLayout
            Me.previewRichEditBoxPanel.SuspendLayout
            DirectCast(Me.headerPictureBox, ISupportInitialize).BeginInit
            Me.okCancelTableLayoutPanel.SuspendLayout
            MyBase.SuspendLayout
            manager.ApplyResources(Me.cancelButton, "cancelButton")
            Me.cancelButton.CausesValidation = False
            Me.cancelButton.DialogResult = DialogResult.Cancel
            Me.cancelButton.Name = "cancelButton"
            AddHandler Me.cancelButton.Click, New EventHandler(AddressOf Me.OnCancel)
            manager.ApplyResources(Me.okButton, "okButton")
            Me.okButton.Name = "okButton"
            AddHandler Me.okButton.Click, New EventHandler(AddressOf Me.OnOk)
            Me.rulesListView.Columns.AddRange(New ColumnHeader() { Me.nameColumnHeader, Me.validColumnHeader })
            manager.ApplyResources(Me.rulesListView, "rulesListView")
            Me.rulesListView.FullRowSelect = True
            Me.rulesListView.HeaderStyle = ColumnHeaderStyle.Nonclickable
            Me.rulesListView.HideSelection = False
            Me.rulesListView.MultiSelect = False
            Me.rulesListView.Name = "rulesListView"
            Me.rulesListView.Sorting = SortOrder.Ascending
            Me.rulesListView.UseCompatibleStateImageBehavior = False
            Me.rulesListView.View = View.Details
            AddHandler Me.rulesListView.DoubleClick, New EventHandler(AddressOf Me.OnDoubleClick)
            AddHandler Me.rulesListView.ItemSelectionChanged, New ListViewItemSelectionChangedEventHandler(AddressOf Me.OnItemSelectionChanged)
            manager.ApplyResources(Me.nameColumnHeader, "nameColumnHeader")
            manager.ApplyResources(Me.validColumnHeader, "validColumnHeader")
            manager.ApplyResources(Me.rulesPanel, "rulesPanel")
            Me.rulesPanel.Controls.Add(Me.rulesToolStrip)
            Me.rulesPanel.Controls.Add(Me.rulesListView)
            Me.rulesPanel.Name = "rulesPanel"
            Me.rulesToolStrip.BackColor = SystemColors.Control
            Me.rulesToolStrip.GripStyle = ToolStripGripStyle.Hidden
            Me.rulesToolStrip.ImageList = Me.imageList
            Me.rulesToolStrip.Items.AddRange(New ToolStripItem() { Me.newRuleToolStripButton, Me.editToolStripButton, Me.renameToolStripButton, Me.toolStripSeparator1, Me.deleteToolStripButton })
            manager.ApplyResources(Me.rulesToolStrip, "rulesToolStrip")
            Me.rulesToolStrip.Name = "rulesToolStrip"
            Me.rulesToolStrip.RenderMode = ToolStripRenderMode.System
            Me.rulesToolStrip.TabStop = True
            Me.imageList.ImageStream = DirectCast(manager.GetObject("imageList.ImageStream"), ImageListStreamer)
            Me.imageList.TransparentColor = Color.Transparent
            Me.imageList.Images.SetKeyName(0, "NewRule.bmp")
            Me.imageList.Images.SetKeyName(1, "EditRule.bmp")
            Me.imageList.Images.SetKeyName(2, "RenameRule.bmp")
            Me.imageList.Images.SetKeyName(3, "Delete.bmp")
            manager.ApplyResources(Me.newRuleToolStripButton, "newRuleToolStripButton")
            Me.newRuleToolStripButton.Name = "newRuleToolStripButton"
            AddHandler Me.newRuleToolStripButton.Click, New EventHandler(AddressOf Me.OnNew)
            manager.ApplyResources(Me.editToolStripButton, "editToolStripButton")
            Me.editToolStripButton.Name = "editToolStripButton"
            AddHandler Me.editToolStripButton.Click, New EventHandler(AddressOf Me.OnEdit)
            manager.ApplyResources(Me.renameToolStripButton, "renameToolStripButton")
            Me.renameToolStripButton.Name = "renameToolStripButton"
            AddHandler Me.renameToolStripButton.Click, New EventHandler(AddressOf Me.OnRename)
            Me.toolStripSeparator1.Name = "toolStripSeparator1"
            manager.ApplyResources(Me.toolStripSeparator1, "toolStripSeparator1")
            manager.ApplyResources(Me.deleteToolStripButton, "deleteToolStripButton")
            Me.deleteToolStripButton.Name = "deleteToolStripButton"
            AddHandler Me.deleteToolStripButton.Click, New EventHandler(AddressOf Me.OnDelete)
            Me.preiviewPanel.Controls.Add(Me.previewRichEditBoxPanel)
            Me.preiviewPanel.Controls.Add(Me.previewLabel)
            manager.ApplyResources(Me.preiviewPanel, "preiviewPanel")
            Me.preiviewPanel.Name = "preiviewPanel"
            manager.ApplyResources(Me.previewRichEditBoxPanel, "previewRichEditBoxPanel")
            Me.previewRichEditBoxPanel.BackColor = SystemColors.GradientInactiveCaption
            Me.previewRichEditBoxPanel.Controls.Add(Me.previewRichTextBox)
            Me.previewRichEditBoxPanel.Name = "previewRichEditBoxPanel"
            Me.previewRichTextBox.BackColor = SystemColors.Control
            Me.previewRichTextBox.BorderStyle = BorderStyle.None
            manager.ApplyResources(Me.previewRichTextBox, "previewRichTextBox")
            Me.previewRichTextBox.Name = "previewRichTextBox"
            Me.previewRichTextBox.ReadOnly = True
            Me.previewRichTextBox.TabStop = False
            manager.ApplyResources(Me.previewLabel, "previewLabel")
            Me.previewLabel.Name = "previewLabel"
            manager.ApplyResources(Me.descriptionLabel, "descriptionLabel")
            Me.descriptionLabel.Name = "descriptionLabel"
            manager.ApplyResources(Me.headerPictureBox, "headerPictureBox")
            Me.headerPictureBox.Name = "headerPictureBox"
            Me.headerPictureBox.TabStop = False
            manager.ApplyResources(Me.okCancelTableLayoutPanel, "okCancelTableLayoutPanel")
            Me.okCancelTableLayoutPanel.Controls.Add(Me.okButton, 0, 0)
            Me.okCancelTableLayoutPanel.Controls.Add(Me.cancelButton, 1, 0)
            Me.okCancelTableLayoutPanel.Name = "okCancelTableLayoutPanel"
            MyBase.AcceptButton = Me.okButton
            manager.ApplyResources(Me, "$this")
            MyBase.CancelButton = Me.cancelButton
            MyBase.Controls.Add(Me.okCancelTableLayoutPanel)
            MyBase.Controls.Add(Me.headerPictureBox)
            MyBase.Controls.Add(Me.descriptionLabel)
            MyBase.Controls.Add(Me.preiviewPanel)
            MyBase.Controls.Add(Me.rulesPanel)
            MyBase.FormBorderStyle = FormBorderStyle.FixedDialog
            MyBase.HelpButton = True
            MyBase.MaximizeBox = False
            MyBase.MinimizeBox = False
            MyBase.Name = "BasicBrowserDialog"
            MyBase.ShowInTaskbar = False
            Me.rulesPanel.ResumeLayout(False)
            Me.rulesPanel.PerformLayout
            Me.rulesToolStrip.ResumeLayout(False)
            Me.rulesToolStrip.PerformLayout
            Me.preiviewPanel.ResumeLayout(False)
            Me.preiviewPanel.PerformLayout
            Me.previewRichEditBoxPanel.ResumeLayout(False)
            Me.previewRichEditBoxPanel.PerformLayout
            DirectCast(Me.headerPictureBox, ISupportInitialize).EndInit
            Me.okCancelTableLayoutPanel.ResumeLayout(False)
            Me.okCancelTableLayoutPanel.PerformLayout
            MyBase.ResumeLayout(False)
            MyBase.PerformLayout
        End Sub

        Protected Sub InitializeListView(ByVal list As IList, ByVal selectedName As String)
            Dim obj2 As Object
            For Each obj2 In list
                Dim listViewItem As ListViewItem = Me.rulesListView.Items.Add(New ListViewItem)
                Me.UpdateListViewItem(obj2, listViewItem)
                If (Me.GetObjectName(obj2) = selectedName) Then
                    listViewItem.Selected = True
                End If
            Next
            If (Me.rulesListView.SelectedItems.Count = 0) Then
                Me.OnToolbarStatus
            End If
        End Sub

        Friend MustOverride Function IsUniqueName(ByVal ruleName As String) As Boolean

        Private Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs)
            Me.name = Nothing
            MyBase.DialogResult = DialogResult.Cancel
            MyBase.Close
        End Sub

        Private Sub OnComponentChanged()
            Dim serviceProvider As ISite = Me.activity.Site
            Dim service As IComponentChangeService = DirectCast(serviceProvider.GetService(GetType(IComponentChangeService)), IComponentChangeService)
            If (Not service Is Nothing) Then
                service.OnComponentChanged(Me.activity, Nothing, Nothing, Nothing)
            End If
            ConditionHelper.Flush_Rules_DT(serviceProvider, Helpers.GetRootActivity(Me.activity))
        End Sub

        Private Function OnComponentChanging() As Boolean
            Dim service As IComponentChangeService = DirectCast(Me.activity.Site.GetService(GetType(IComponentChangeService)), IComponentChangeService)
            If (Not service Is Nothing) Then
                Try 
                    service.OnComponentChanging(Me.activity, Nothing)
                Catch exception As CheckoutException
                    If (Not exception Is CheckoutException.Canceled) Then
                        Throw
                    End If
                    Return False
                End Try
            End If
            Return True
        End Function

        Private Sub OnDelete(ByVal sender As Object, ByVal e As EventArgs)
            Dim options As MessageBoxOptions = 0
            If CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft Then
                options = (MessageBoxOptions.RtlReading Or MessageBoxOptions.RightAlign)
            End If
            If (MessageBox.Show(Me, Me.ConfirmDeleteMessageText, Me.ConfirmDeleteTitleText, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, options) = DialogResult.OK) Then
                [Using] New WaitCursor
                    Dim tag As Object = Me.rulesListView.SelectedItems.Item(0).Tag
                    Try 
                        Me.OnComponentChanging
                        Dim index As Integer = Me.rulesListView.SelectedIndices.Item(0)
                        Dim ruleObject As Object = Nothing
                        Me.OnDeleteInternal(tag)
                        Me.rulesListView.Items.RemoveAt(index)
                        If (Me.rulesListView.Items.Count > 0) Then
                            Dim num2 As Integer = Math.Min(index, (Me.rulesListView.Items.Count - 1))
                            Me.rulesListView.Items.Item(num2).Selected = True
                            ruleObject = Me.rulesListView.Items.Item(num2).Tag
                        End If
                        Me.UpdatePreview(Me.previewRichTextBox, ruleObject)
                        Me.OnComponentChanged
                    Catch exception As InvalidOperationException
                        DesignerHelpers.DisplayError(exception.Message, Me.Text, Me.activity.Site)
                    End Try
                End [Using]
            End If
        End Sub

        Protected MustOverride Sub OnDeleteInternal(ByVal ruleObject As Object)

        Private Sub OnDoubleClick(ByVal sender As Object, ByVal e As EventArgs)
            If (Me.rulesListView.SelectedItems.Count > 0) Then
                Me.OnOk(sender, e)
            End If
        End Sub

        Private Sub OnEdit(ByVal sender As Object, ByVal e As EventArgs)
            Try 
                Me.OnComponentChanging
                Dim updatedRuleObject As Object = Nothing
                Dim tag As Object = Me.rulesListView.SelectedItems.Item(0).Tag
                If Me.OnEditInternal(tag, updatedRuleObject) Then
                    [Using] New WaitCursor
                        Me.UpdateListViewItem(updatedRuleObject, Me.rulesListView.SelectedItems.Item(0))
                        Me.UpdatePreview(Me.previewRichTextBox, updatedRuleObject)
                        Me.OnComponentChanged
                    End [Using]
                End If
            Catch exception As InvalidOperationException
                DesignerHelpers.DisplayError(exception.Message, Me.Text, Me.activity.Site)
            End Try
        End Sub

        Protected MustOverride Function OnEditInternal(ByVal currentRuleObject As Object, <Out> ByRef updatedRuleObject As Object) As Boolean

        Private Sub OnHelpClicked(ByVal sender As Object, ByVal e As CancelEventArgs)
            e.Cancel = True
            Me.ShowHelp
        End Sub

        Private Sub OnHelpRequested(ByVal sender As Object, ByVal e As HelpEventArgs)
            Me.ShowHelp
        End Sub

        Private Sub OnItemSelectionChanged(ByVal sender As Object, ByVal e As ListViewItemSelectionChangedEventArgs)
            If e.IsSelected Then
                e.Item.Focused = True
            End If
            Me.OnToolbarStatus
            Me.okButton.Enabled = e.IsSelected
            Dim ruleObject As Object = Nothing
            If e.IsSelected Then
                ruleObject = e.Item.Tag
            End If
            Me.UpdatePreview(Me.previewRichTextBox, ruleObject)
        End Sub

        Private Sub OnNew(ByVal sender As Object, ByVal e As EventArgs)
            Try 
                Me.OnComponentChanging
                Dim ruleObject As Object = Me.OnNewInternal
                If (Not ruleObject Is Nothing) Then
                    [Using] New WaitCursor
                        Dim listViewItem As ListViewItem = Me.rulesListView.Items.Add(New ListViewItem)
                        Me.UpdateListViewItem(ruleObject, listViewItem)
                        listViewItem.Selected = True
                        Me.OnComponentChanged
                    End [Using]
                End If
            Catch exception As InvalidOperationException
                DesignerHelpers.DisplayError(exception.Message, Me.Text, Me.activity.Site)
            End Try
        End Sub

        Protected MustOverride Function OnNewInternal() As Object

        Private Sub OnOk(ByVal sender As Object, ByVal e As EventArgs)
            Dim tag As Object = Me.rulesListView.SelectedItems.Item(0).Tag
            Me.name = Me.GetObjectName(tag)
            MyBase.DialogResult = DialogResult.OK
            MyBase.Close
        End Sub

        Private Sub OnRename(ByVal sender As Object, ByVal e As EventArgs)
            Try 
                Me.OnComponentChanging
                Dim tag As Object = Me.rulesListView.SelectedItems.Item(0).Tag
                Dim str As String = Me.OnRenameInternal(tag)
                If (Not str Is Nothing) Then
                    [Using] New WaitCursor
                        Dim item As ListViewItem = Me.rulesListView.SelectedItems.Item(0)
                        item.Text = str
                        Me.OnComponentChanged
                    End [Using]
                End If
            Catch exception As InvalidOperationException
                DesignerHelpers.DisplayError(exception.Message, Me.Text, Me.activity.Site)
            End Try
        End Sub

        Protected MustOverride Function OnRenameInternal(ByVal ruleObject As Object) As String

        Private Sub OnToolbarStatus()
            If (Me.rulesListView.SelectedItems.Count = 1) Then
                Me.editToolStripButton.Enabled = True
                Me.renameToolStripButton.Enabled = True
                Me.deleteToolStripButton.Enabled = True
            Else
                Me.editToolStripButton.Enabled = False
                Me.renameToolStripButton.Enabled = False
                Me.deleteToolStripButton.Enabled = False
            End If
        End Sub

        Private Sub ShowHelp()
            If (Not Me.serviceProvider Is Nothing) Then
                Dim service As IHelpService = [TryCast](Me.serviceProvider.GetService(GetType(IHelpService)),IHelpService)
                If (Not service Is Nothing) Then
                    service.ShowHelpFromKeyword((MyBase.GetType.FullName & ".UI"))
                Else
                    Dim service2 As IUIService = [TryCast](Me.serviceProvider.GetService(GetType(IUIService)),IUIService)
                    If (Not service2 Is Nothing) Then
                        service2.ShowError(Messages.NoHelp)
                    End If
                End If
            Else
                Dim service3 As IUIService = DirectCast(Me.GetService(GetType(IUIService)), IUIService)
                If (Not service3 Is Nothing) Then
                    service3.ShowError(Messages.NoHelp)
                End If
            End If
        End Sub

        Protected MustOverride Sub UpdateListViewItem(ByVal ruleObject As Object, ByVal listViewItem As ListViewItem)

        Protected MustOverride Sub UpdatePreview(ByVal previewTextBox As TextBox, ByVal ruleObject As Object)


        ' Properties
        Protected ReadOnly Property Activity As Activity
            Get
                Return Me.activity
            End Get
        End Property

        Protected MustOverride ReadOnly Property ConfirmDeleteMessageText As String

        Protected MustOverride ReadOnly Property ConfirmDeleteTitleText As String

        Protected MustOverride ReadOnly Property DescriptionText As String

        Friend MustOverride ReadOnly Property DuplicateNameErrorText As String

        Friend MustOverride ReadOnly Property EmptyNameErrorText As String

        Friend MustOverride ReadOnly Property NewNameLabelText As String

        Protected MustOverride ReadOnly Property PreviewLabelText As String

        Friend MustOverride ReadOnly Property RenameTitleText As String

        Public ReadOnly Property SelectedName As String
            Get
                Return Me.name
            End Get
        End Property

        Protected MustOverride ReadOnly Property TitleText As String


        ' Fields
        Private activity As Activity
        Private cancelButton As Button
        Private components As IContainer
        Private deleteToolStripButton As ToolStripButton
        Private descriptionLabel As Label
        Private editToolStripButton As ToolStripButton
        Private headerPictureBox As PictureBox
        Private imageList As ImageList
        Private name As String
        Private nameColumnHeader As ColumnHeader
        Private newRuleToolStripButton As ToolStripButton
        Private okButton As Button
        Private okCancelTableLayoutPanel As TableLayoutPanel
        Private preiviewPanel As Panel
        Private previewLabel As Label
        Private previewRichEditBoxPanel As Panel
        Private previewRichTextBox As TextBox
        Private renameToolStripButton As ToolStripButton
        Private rulesListView As ListView
        Private rulesPanel As Panel
        Private rulesToolStrip As ToolStrip
        Private serviceProvider As IServiceProvider
        Private toolStripSeparator1 As ToolStripSeparator
        Private validColumnHeader As ColumnHeader

        ' Nested Types
        Private Class WaitCursor
            Implements IDisposable
            ' Methods
            Public Sub New()
                Application.DoEvents
                Me.oldCursor = Cursor.Current
                Cursor.Current = Cursors.WaitCursor
            End Sub

            Public Sub Dispose()
                Cursor.Current = Me.oldCursor
            End Sub


            ' Fields
            Private oldCursor As Cursor
        End Class
    End Class
End Namespace

